package com.ias.agdis_r.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.daimajia.swipe.SimpleSwipeListener;
import com.daimajia.swipe.SwipeLayout;
import com.daimajia.swipe.adapters.BaseSwipeAdapter;
import com.ias.agdis_r.R;
import com.ias.agdis_r.bean.task.LuggageBarCode;
import com.ias.agdis_r.module.MessageModel;
import com.ias.agdis_r.terminalmanager.restful.task.endpoint.request.DeleteLuggageRequest;
import com.ias.agdis_r.util.network.HttpUtil;

import java.util.List;

/**
 * Created by xr.g on 2018/2/6.
 */

public class LuggageBarCodeAdapter extends BaseSwipeAdapter {
    private Context context;
    private List<LuggageBarCode> list;
    private boolean enableSwip;

    public LuggageBarCodeAdapter(Context context, List<LuggageBarCode> list, boolean enableSwipe) {
        this.context = context;
        this.list = list;
        this.enableSwip = enableSwipe;
    }

    @Override
    public int getSwipeLayoutResourceId(int position) {
        return R.id.swipe;
    }

    @Override
    public View generateView(final int position, ViewGroup parent) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_luggagebarcode, null);
        if (!enableSwip) {
            final SwipeLayout swipeLayout = (SwipeLayout) view.findViewById(getSwipeLayoutResourceId(position));
            // 设置侧滑不可用
            swipeLayout.setSwipeEnabled(false);
        }
        return view;
    }

    @Override
    public void fillValues(final int position, View convertView) {
        final LuggageBarCode luggageBarCode = list.get(position);
        if (null != luggageBarCode) {
            final SwipeLayout swipeLayout = (SwipeLayout) convertView.findViewById(getSwipeLayoutResourceId(position));
            swipeLayout.close(false, false);
            // 添加删除布局的点击事件
            convertView.findViewById(R.id.item_delete).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    DeleteLuggageRequest deleteLuggageRequest = new DeleteLuggageRequest();
                    deleteLuggageRequest.setBarcode(luggageBarCode.getBarcode());
                    deleteLuggageRequest.setAdid(luggageBarCode.getAdid());
                    deleteLuggageRequest.setEnb(luggageBarCode.getEnb());
                    deleteLuggageRequest.setFkey(luggageBarCode.getFkey());
                    deleteLuggageRequest.setTnb(luggageBarCode.getTnb());
                    HttpUtil.getInstance().sendRequest(deleteLuggageRequest, MessageModel.DLBC, true);
                    swipeLayout.close();
                }
            });
            TextView num = (TextView) convertView.findViewById(R.id.luggageBarCodeNum);
            num.setText(luggageBarCode.getBarcode());
            TextView tvNo = (TextView) convertView.findViewById(R.id.luggageBarCodeNo);
            tvNo.setText(String.valueOf(position + 1));
            ImageView state = (ImageView) convertView.findViewById(R.id.luggageBarCodeState);
            if (LuggageBarCode.STATE_ADD.equals(luggageBarCode.getState())) {
                state.setImageResource(R.drawable.barcodestate_add);
            } else if (LuggageBarCode.STATE_SENDING.equals(luggageBarCode.getState())) {
                state.setImageResource(R.drawable.barcodestate_sending);
            } else if (LuggageBarCode.STATE_SEND_FAILED.equals(luggageBarCode.getState())) {
                state.setImageResource(R.drawable.barcodestate_send_failed);
            } else if (LuggageBarCode.STATE_SENT.equals(luggageBarCode.getState())) {
                state.setImageResource(R.drawable.barcodestate_sent);
            } else if (LuggageBarCode.STATE_DELETE_FAILED.equals(luggageBarCode.getState())) {
                state.setImageResource(R.drawable.barcodestate_delete_failed);
            }
        }
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public LuggageBarCode getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
}
